/**
 * 
 */
package edu.mse.camel.client.posterboard.ui.outline;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

class ViewportFigure extends Figure {
	
	protected Image image;
	protected Color border;
	protected RGB rgb;
	
	public ViewportFigure(RGB rgb) {
		this.rgb = rgb;
		Display display = Display.getCurrent();
		PaletteData pData = new PaletteData(0xFF, 0xFF00, 0xFF0000);
		int fillColor = pData.getPixel(rgb);
		ImageData iData = new ImageData(1, 1, 24, pData);
		iData.setPixel(0, 0, fillColor);
		iData.setAlpha(0, 0, 55);
		image = new Image(display, iData);
		border = new Color(display, rgb);
	}
	
	protected Rectangle iBounds = new Rectangle(0, 0, 1, 1);
	
	protected void dispose() {
		image.dispose();
	}
	
	public void paintFigure(Graphics g) {
		Rectangle bounds = getBounds().getCopy();		

		// Avoid drawing images that are 0 in dimension
		if (bounds.width < 5 || bounds.height < 5)
			return;
		
		bounds.height--;
		bounds.width--;
		g.drawImage(image, iBounds, bounds);
		
		g.setForegroundColor(border);
		g.drawRectangle(bounds);
	}

	public void setFigureColor(RGB rgb) {
		this.rgb = rgb;
		Image oldImage = image;
		PaletteData pData = new PaletteData(0xFF, 0xFF00, 0xFF0000);
		int fillColor = pData.getPixel(rgb);
		ImageData iData = new ImageData(1, 1, 24, pData);
		iData.setPixel(0, 0, fillColor);
		iData.setAlpha(0, 0, 55);
		image = new Image(Display.getCurrent(), iData);
		border = new Color(Display.getCurrent(), rgb);
		oldImage.dispose();
	}
	
}